package com.chen.controller;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.chen.common.Result;
import com.chen.mapper.NewsMapper;
import com.chen.mapper.UserMapper;
import com.chen.pojo.News;
import com.chen.pojo.User;
import com.chen.service.Impl.NewsServiceImpl;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@SuppressWarnings("all")
@RequestMapping("/news")
@Api(value = "newsController", tags = "博客")
public class NewsController {
    @Resource
    private NewsServiceImpl newsService;
    @Resource
    private UserMapper userMapper;
    @Resource
    private NewsMapper newsMapper;

    @PostMapping("/addNew")
    @ApiOperation("添加博客")
    public Result<?> addNews(@RequestBody News news) {
        return newsService.addNews(news);
    }

    @PutMapping("/updateNew")
    @ApiOperation("更新博客")
    public Result<?> updateNews(@RequestBody News news) {
        return newsService.updateNews(news);
    }


    @DeleteMapping("/deleteNew/{id}")
    @ApiOperation("删除博客")
    public Result<?> deleteNews(@PathVariable("id") Long id) {
        return newsService.delete(id);
    }

    @GetMapping("/query/{id}")
    @ApiOperation("查询")
    public Result<?> getById(@PathVariable("id") Long id) {
        return newsService.getById(id);
    }

    @GetMapping
    @ApiOperation("查询所有留言")
    public Result<?> findAll() {

        User user = new User();
        List<News> news = newsMapper.selectList(null);
        for (News news1 : news) {
            User one = userMapper.selectOne(Wrappers.<User>lambdaQuery().eq(User::getNick, news1.getAuthor()));
            if (ObjectUtil.isNotEmpty(one)) {
                if (StrUtil.isNotBlank(one.getAvatar())) {
                    news1.setAvatar(one.getAvatar());
                }else {
                    // 默认一个头像
                    news1.setAvatar("https://img2.baidu.com/it/u=2906052251,1830965798&fm=26&fmt=auto");
                }
            }
        }

        return Result.success(news);    
    }


    @GetMapping("/selectPage")
    @ApiOperation("分页")
    public Result<?> findPage(@RequestParam(defaultValue = "1") Integer pageNum,
                              @RequestParam(defaultValue = "5") Integer pageSize,
                              @RequestParam(defaultValue = "") String search) {
        //模糊查询
        LambdaQueryWrapper<News> wrapper = Wrappers.<News>lambdaQuery().like(News::getAuthor, search);
        if (StrUtil.isBlank(search)) {
            wrapper.like(News::getAuthor, search);
        }
        return newsService.findPage(pageNum, pageSize, search);

    }

    @GetMapping("/echarts")
    @ApiOperation("echarts图表")
    public Result<?> findTypes(){
        System.out.println(newsService.finfType());
        return Result.success(newsService.finfType());
    }

}
